package com.site.dao.impl;

import com.site.dao.CircuitBoardSizeDao;
import com.site.pojo.CircuitBoardSize;
import com.site.utils.JDBCUtils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * FileName: CircuitBoardSizeDaoImpl.java
 * 电路板规格表DaoImpl
 *
 * @author 田尚忠
 * @Date    2021/11/28
 * @version 1.00
 */
public class CircuitBoardSizeDaoImpl implements CircuitBoardSizeDao {

    //查询全部
    @Override
    public List<CircuitBoardSize> selectAll() throws SQLException {
        List<CircuitBoardSize> list = new ArrayList<>();
        Connection conn = JDBCUtils.getConnection();
        String sql = "SELECT `type_id`, `goods_id`, `c_id`, `with`, `height`, `describe` FROM `circuit_board_size`";
        ResultSet rs = JDBCUtils.executeQuery(conn, sql);
        while (rs.next()){
            CircuitBoardSize circuitBoardSize = new CircuitBoardSize();
            circuitBoardSize.setTypeId(rs.getLong("type_id"));
            circuitBoardSize.setGoodsId(rs.getLong("goods_id"));
            circuitBoardSize.setCId(rs.getLong("c_id"));
            circuitBoardSize.setWith(rs.getString("with"));
            circuitBoardSize.setHeight(rs.getString("height"));
            circuitBoardSize.setDescribe(rs.getString("describe"));
            list.add(circuitBoardSize);
        }
        JDBCUtils.close(rs,null,conn);
        return list;
    }

    //按照id进行查询
    @Override
    public CircuitBoardSize selectByid(int id) throws SQLException {
        Connection conn = JDBCUtils.getConnection();
        CircuitBoardSize circuitBoardSize = new CircuitBoardSize();
        String sql = "SELECT `type_id`, `goods_id`, `c_id`, `with`, `height`, `describe` FROM `circuit_board_size` where c_id=?";
        ResultSet rs = JDBCUtils.executeQuery(conn,sql,id);
        while (rs.next()){
            circuitBoardSize.setTypeId(rs.getLong("type_id"));
            circuitBoardSize.setGoodsId(rs.getLong("goods_id"));
            circuitBoardSize.setCId(rs.getLong("c_id"));
            circuitBoardSize.setWith(rs.getString("with"));
            circuitBoardSize.setHeight(rs.getString("height"));
            circuitBoardSize.setDescribe(rs.getString("describe"));
        }
        JDBCUtils.close(rs,null,conn);
        return circuitBoardSize;
    }

    //按照商品id进行查询
    @Override
    public List<CircuitBoardSize> selectByGoodsid(int id) throws SQLException {
        List<CircuitBoardSize> list = new ArrayList<>();
        Connection conn = JDBCUtils.getConnection();
        String sql = "SELECT `type_id`, `goods_id`, `c_id`, `with`, `height`, `describe` FROM `circuit_board_size` where goods_id=?";
        ResultSet rs = JDBCUtils.executeQuery(conn,sql,id);
        while (rs.next()){
            CircuitBoardSize circuitBoardSize = new CircuitBoardSize();
            circuitBoardSize.setTypeId(rs.getLong("type_id"));
            circuitBoardSize.setGoodsId(rs.getLong("goods_id"));
            circuitBoardSize.setCId(rs.getLong("c_id"));
            circuitBoardSize.setWith(rs.getString("with"));
            circuitBoardSize.setHeight(rs.getString("height"));
            circuitBoardSize.setDescribe(rs.getString("describe"));
            list.add(circuitBoardSize);
        }
        JDBCUtils.close(rs,null,conn);
        return list;
    }

    //按照信息进行查询是否存在
    @Override
    public CircuitBoardSize selectinfo(CircuitBoardSize circuitBoardSize1) throws SQLException {
        Connection conn = JDBCUtils.getConnection();
        CircuitBoardSize circuitBoardSize = new CircuitBoardSize();
        String sql = "SELECT `type_id`, `goods_id`, `c_id`, `with`, `height`, `describe` FROM `circuit_board_size` WHERE `type_id` = ? AND `goods_id` = ? AND `with` = ? AND `height` = ? AND `describe`=?";
        ResultSet rs = JDBCUtils.executeQuery(conn,sql,circuitBoardSize1.getTypeId(),circuitBoardSize1.getGoodsId(),circuitBoardSize1.getWith(),circuitBoardSize1.getHeight(),circuitBoardSize1.getDescribe());
        while (rs.next()){
            circuitBoardSize.setTypeId(rs.getLong("type_id"));
            circuitBoardSize.setGoodsId(rs.getLong("goods_id"));
            circuitBoardSize.setCId(rs.getLong("c_id"));
            circuitBoardSize.setWith(rs.getString("with"));
            circuitBoardSize.setHeight(rs.getString("height"));
            circuitBoardSize.setDescribe(rs.getString("describe"));
        }
        JDBCUtils.close(rs,null,conn);
        return circuitBoardSize;
    }

    //新增
    @Override
    public int addCircuitBoardSize(CircuitBoardSize circuitBoardSize1) throws SQLException {
        String sql = "INSERT INTO `hurricane_house`.`circuit_board_size` (`type_id`, `goods_id`, `with`, `height`, `describe`) VALUES (?, ?, ?, ?,?);";
        Object [] params = {circuitBoardSize1.getTypeId(),circuitBoardSize1.getGoodsId(),circuitBoardSize1.getWith(),circuitBoardSize1.getHeight(),circuitBoardSize1.getDescribe()};
        return JDBCUtils.executeUpdate(sql, params);
    }

    //有事务的新增
    @Override
    public int addCircuitBoardSize(Connection conn, CircuitBoardSize cableSize) {
        String sql = "INSERT INTO `hurricane_house`.`circuit_board_size` (`type_id`, `goods_id`, `with`, `height`, `describe`) VALUES (?, ?, ?, ?,?);";
        Object [] params = {cableSize.getTypeId(),cableSize.getGoodsId(),cableSize.getWith(),cableSize.getHeight(),cableSize.getDescribe()};
        return JDBCUtils.executeUpdate(conn,sql, params);
    }

    //按照id进行删除
    @Override
    public int delCircuitBoardSizeByid(int id) {
        String sql = "DELETE FROM circuit_board_size WHERE c_id = ?";
        Object [] params = {id};
        return JDBCUtils.executeUpdate(sql, params);
    }

    //有事务的进行按照id删除
    @Override
    public int delCircuitBoardSizeByid(Connection conn, int id) {
        String sql = "DELETE FROM circuit_board_size WHERE c_id = ?";
        Object [] params = {id};
        return JDBCUtils.executeUpdate(conn,sql, params);
    }

    //按照商品id进行删除
    @Override
    public int delCircuitBoardSizeByGoodsid(int id) {
        String sql = "DELETE FROM circuit_board_size WHERE goods_id = ?";
        Object [] params = {id};
        return JDBCUtils.executeUpdate(sql, params);
    }

    //有事务的按照商品id进行删除
    @Override
    public int delCircuitBoardSizeByGoodsid(Connection conn, int id) {
        String sql = "DELETE FROM circuit_board_size WHERE goods_id = ?";
        Object [] params = {id};
        return JDBCUtils.executeUpdate(conn,sql, params);
    }

    //修改商品信息
    @Override
    public int updateCircuitBoardSize(int id,CircuitBoardSize cableSize) {
        String sql = "UPDATE `hurricane_house`.`circuit_board_size` SET `type_id` = ?, `goods_id` = ?, `with` = ?, `height` = ?, `describe` = ? WHERE `c_id` = ?;";
        Object [] params = {cableSize.getTypeId(),cableSize.getGoodsId(),cableSize.getWith(),cableSize.getHeight(),cableSize.getDescribe(),id};
        return JDBCUtils.executeUpdate(sql, params);
    }

    @Override
    public int updateCircuitBoardSize(Connection conn, int id, CircuitBoardSize circuitBoardSize) {
        String sql = "UPDATE `hurricane_house`.`circuit_board_size` SET `type_id` = ?, `goods_id` = ?, `with` = ?, `height` = ?, `describe` = ? WHERE `c_id` = ?;";
        Object [] params = {circuitBoardSize.getTypeId(),circuitBoardSize.getGoodsId(),circuitBoardSize.getWith(),circuitBoardSize.getHeight(),circuitBoardSize.getDescribe(),id};
        return JDBCUtils.executeUpdate(conn,sql, params);
    }
}
